******************************
****					  ****
****     FATA PROJECT     ****
****      (analysis)	  ****
****					  ****
******************************


global PathData = "/Users/krzysztofkrakowski/Dropbox/FATA_fractionalization/1_Data/"
global PathRes = "/Users/krzysztofkrakowski/Dropbox/FATA_fractionalization/4_Results/"

use "${PathData}fata_data_min_recoded.dta", clear
rename vill_code vill_code_wrong

merge m:1 tehs_code using "${PathData}tehs_segr_min.dta", force nogenerate
merge m:1 vill_code_wrong using "${PathData}pop_landscan.dta", nogenerate force

recode sex 1=1 0=0, gen(man) 
recode education 0 =1 1/4=2 5/6=3, gen(edu_rec) 
tab edu_rec, gen(educ)
egen victimization = rowtotal(death_sa kidnap_sa fmkidnap_sa ppd_sa fmpd_sa hd_sa ld_sa idp_sa fidp_sa ext_sa fmext_sa) 
egen chronicly_ill = rowtotal(phy_disable per_mentdisb phy_illness)
recode marital_stat 0=1 1/4=0, gen(never_married)
gen lnmont_incm = ln(mont_incm+1)
recode religion 1=0 2=1, gen(shia)
bys vill_code: egen shia_percent = mean(shia)
zval minority_vill minority_tehs Cseg
gen frac_village_minority = z_tribal_frac0*z_minority_vill
gen frac_tehsil_minority = z_Cseg*z_minority_tehs


label variable tribal_frac0 "Fractionalization"
label variable Cseg "Segregation"
label variable z_tribal_frac0 "Fractionalization"
label variable z_Cseg "Segregation"
label variable man "Male"
label variable age "Age"
label variable educ1 "No education"
label variable educ2 "Primary / secondary"
label variable educ3 "Tertiary"
label variable victimization "Victimization index"
label variable chronicly_ill "Illness index"
label variable lnmont_incm "Monthly income (log)"
label variable never_married "Never married"
label variable shia "Shia"
label variable shia_percent "Percent Shia"
label variable minority_vill "Minority (village)"
label variable minority_tehs "Minority (tehsil)"
label variable z_minority_vill "Minority (village)"
label variable z_minority_tehs "Minority (tehsil)"
label variable frac_village_minority "Village frac. x Minority"
label variable frac_tehsil_minority "Tehsil segr. x Minority"

egen services_all = rowmean(rebuilding med_services edu infra water sanitation agri_supp busi_supp bank_ser reli_ser justice security)
egen services = rowmean(rebuilding med_services edu infra water sanitation agri_supp busi_supp bank_ser reli_ser)
egen services_security = rowmean(justice security)
zval services services_security services_all

	replace re_comm=0 if re_comm==.
	replace ms_comm=0 if ms_comm==.
	replace edu_comm=0 if edu_comm==. 
	replace infra_comm=0 if infra_comm==.  
	replace wat_comm=0 if wat_comm==. 
	replace sani_comm=0 if sani_comm==. 
	replace just_comm=0 if just_comm==. 
	replace sec_comm=0 if sec_comm==. 
	replace as_comm=0 if as_comm==.
	replace bs_comm=0 if bs_comm==.
	replace bank_comm=0 if bank_comm==. 
	replace rs_comm=0 if rs_comm==.

	replace re_govt=0 if re_govt==. & re_dk!=6
	replace ms_govt=0 if ms_govt==. & ms_dk!=6
	replace edu_govt=0 if edu_govt==. & edu_dk!=6
	replace infra_govt=0 if infra_govt==. & infra_dk!=6
	replace wat_govt=0 if wat_govt==. & wat_dk!=6
	replace sani_govt=0 if sani_govt==. & sani_dk!=6
	replace just_govt=0 if just_govt==.& just_dk!=6
	replace sec_govt=0 if sec_govt==. & sec_dk!=6
	replace as_govt=0 if as_govt==. & as_dk!=6
	replace bs_govt=0 if bs_govt==. & bs_dk!=6
	replace bank_govt=0 if bank_govt==. & bank_dk!=6
	replace rs_govt=0 if rs_govt==. & rs_dk!=6	

	replace re_army=0 if re_army==.& re_dk!=6
	replace ms_army=0 if ms_army==.& ms_dk!=6
	replace edu_army=0 if edu_army==.& edu_dk!=6	
	replace infra_army=0 if infra_army==.& infra_dk!=6	
	replace wat_army=0 if wat_army==.& wat_dk!=6	
	replace sani_army=0 if sani_army==.& sani_dk!=6	
	replace just_army=0 if just_army==.& just_dk!=6	
	replace sec_army=0 if sec_army==.& sec_dk!=6	
	replace as_army=0 if as_army==.& as_dk!=6	
	replace bs_army=0 if bs_army==.& bs_dk!=6	
	replace bank_army=0 if bank_army==. & bank_dk!=6
	replace rs_army=0 if rs_army==. & rs_dk!=6

egen service_govt_temp = rowmean(govt_repr re_govt ms_govt edu_govt infra_govt wat_govt sani_govt as_govt bs_govt bank_govt rs_govt)
gen service_govt = service_govt_temp/2
drop service_govt_temp
egen service_army_temp = rowmean(re_army ms_army edu_army infra_army wat_army sani_army as_army bs_army bank_army rs_army)
gen service_army = service_army_temp/3
drop service_army_temp
zval  service_govt service_army
egen service_state = rowmean(service_govt service_army)
zval service_state

egen security_govt_temp = rowmean(just_govt sec_govt)
gen security_govt = security_govt_temp/2
drop security_govt_temp
egen security_army_temp = rowmean(just_army sec_army)
gen security_army = security_army_temp/3
drop security_army_temp
zval  security_govt security_army
egen security_state = rowmean(security_govt security_army)
zval security_state


egen ngo_justice = rowmean(ngos_dr)
zval ngo_justice
label variable z_ngo_justice "NGO dispute"

gen re_imp2=re_imp if re_govt==2 
	gen ms_imp2=ms_imp if ms_govt==2 
	gen edu_imp2=edu_imp if edu_govt==2 
	gen infra_imp2=infra_imp if infra_govt==2 
	gen wat_imp2=wat_imp if wat_govt==2 
	replace wat_imp2=1 if wat_imp2==0
	gen sani_imp2=sani_imp if sani_govt==2 
	gen just_imp2=just_imp if just_govt==2 
	gen as_imp2=as_imp if as_govt==2 
	gen bs_imp2=bs_imp if bs_govt==2 
	gen bank_imp2=bank_imp if bank_govt==2 
	gen rs_imp2=rs_imp if rs_govt==2 

egen service_govt_imp_temp = rowmean(re_imp2 ms_imp2 edu_imp2 infra_imp2 wat_imp2 sani_imp2 as_imp2 bs_imp2 bank_imp2 rs_imp2 ) 
gen service_govt_imp = service_govt_imp_temp*(-1)
zval service_govt_imp 



drop vill_pop
gen count = 1
bys vill_code: egen vill_pop = sum(count) 
bys vill_code: gen count2=_n 
reg value vill_pop if count2==1 & value!=0
predict pop_imputed
gen pop_imputed2 = 2203.172 + (64.78758 * vill_pop)
gen lnpop = ln(pop_imputed2)



pca taxes1 enforcement1
predict pca1 pca2



gen welfare = taxes1-enforcement1
zval welfare




gen z_inter = z_tribal_frac0*z_Cseg
label variable z_inter "Frac. x Segr."
label variable pca2 "Welfere vs Security (PCA)"
label variable z_Cseg "Segregation"
label variable z_tribal_frac0 "Fractionalization"








******************************
****     MAIN TABLE       ****
******************************



* Tab A13
eststo clear
eststo Welfare:  reghdfe  z_welfare  z_tribal_frac0 z_Cseg  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs frac_village_minority frac_tehsil_minority  , cl(vill_code tehs_code) abs(tribe_name) 
estadd local controls = "Yes" 
eststo Welfare_2:  reghdfe  z_service_govt_imp  z_tribal_frac0 z_Cseg  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs frac_village_minority frac_tehsil_minority  , cl(vill_code tehs_code) abs(tribe_name) 
estadd local controls = "Yes"  
eststo Security:  reghdfe  z_enforcement1  z_tribal_frac0 z_Cseg  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs frac_village_minority frac_tehsil_minority  , cl(vill_code tehs_code) abs(tribe_name)
estadd local controls = "Yes"  
eststo Welf_Vs_Sec_1:  reghdfe  pca2  z_tribal_frac0 z_Cseg  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs frac_village_minority frac_tehsil_minority   , cl(vill_code tehs_code) abs(tribe_name) 
estadd local controls = "Yes"  
eststo Welf_Vs_Sec_2:  reghdfe  pca2  z_tribal_frac0 z_Cseg z_inter man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs frac_village_minority frac_tehsil_minority   , cl(vill_code tehs_code) abs(tribe_name) 
estadd local controls = "Yes"  
esttab Welfare Welfare_2 Security Welf_Vs_Sec_1 Welf_Vs_Sec_2 using ${PathRes}table3_min.rtf, b(3) se(3) star(* 0.1 ** 0.05 *** 0.01) drop(_cons) stats( controls r2 N, labels("Controls" "R-squared" "N")) label mtitles title("Tribal Fractionalization, Segregation, and Support for Comprehensive Welfare State vs Security-Oriented State (controls)") replace nonotes




***************************************
****   HETEROGENEITY OF PREFERENC  ****
***************************************


*Fig A2
encode tribe_name, gen(tribe_num)
svyset tehs_code || vill_code
eststo Rebuilding: svy: reg re_imp i.tribe_num
eststo Medical: svy: reg ms_imp i.tribe_num
eststo Education: svy: reg edu_imp i.tribe_num
eststo Infrastructure: svy: reg infra_imp i.tribe_num
eststo Water: svy: reg wat_imp i.tribe_num
eststo Sanitation: svy: reg sani_imp i.tribe_num
eststo Justice: svy: reg just_imp i.tribe_num
eststo Agriculture: svy: reg as_imp i.tribe_num
eststo Business: svy: reg bs_imp i.tribe_num
eststo Banking: svy: reg bank_imp i.tribe_num
eststo Religion: svy: reg rs_imp i.tribe_num
coefplot Rebuilding || Medical || Education || Infrastructure || Water || Sanitation || Justice || Agriculture || Business || Banking || Religion, drop(_cons) xline(0) horizontal bycoefs byopts(xrescale) level(95 90) legend(off) plotregion(lcolor(black)) xlabel(,grid nogextend ) ylabel(,grid nogextend ) aspectratio(1.2)




